package cycling.taxi.com.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.nio.DoubleBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.bean.HistoryInfo;
import cycling.taxi.com.dialog.ActionSheetDialog;
import cycling.taxi.com.entity.Tips;
import cycling.taxi.com.entity.WeekToCount;
import cycling.taxi.com.person.AboutActivity;
import cycling.taxi.com.person.AftersalesActivity;
import cycling.taxi.com.person.FedbackActivity;
import cycling.taxi.com.person.MycollctActivity;
import cycling.taxi.com.person.SettingActivity;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.ui.activity.BindCarAty;
import cycling.taxi.com.ui.activity.DispathActivity;
import cycling.taxi.com.ui.activity.MessageListActivity;
import cycling.taxi.com.ui.activity.StealActivity;
import cycling.taxi.com.ui.activity.Testnew;
import cycling.taxi.com.ui.activity.TrancferActivty;
import cycling.taxi.com.utils.LogUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.RoundBitmap;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;
import cycling.taxi.com.view.CircleImageView;
import cycling.taxi.com.view.statistical.Anim;
import cycling.taxi.com.view.statistical.BarCharts;
import cycling.taxi.com.view.statistical.Histogram;
import cycling.taxi.com.view.statistical.HistogramData;

/**
 * 我的
 * Created by Administrator on 2017/4/20.
 */
public class MyFrgt extends Fragment implements RequestCallback{
    @InjectView(R.id.bind_car)
    LinearLayout mBindCar;
    @InjectView(R.id.my_message)
    LinearLayout mMyMessage;
   /* @InjectView(R.id.my_sales_service)
    LinearLayout mMySalesService;
    @InjectView(R.id.my_collect)*/
    LinearLayout mMyCollect;
//    @InjectView(R.id.my_feedback)
//    LinearLayout mMyFeedback;
    @InjectView(R.id.my_setting)
    LinearLayout mMySetting;
    @InjectView(R.id.my_about)
    LinearLayout mMyAbout;
    @InjectView(R.id.my_icon)
    CircleImageView mIcon;
    @InjectView(R.id.my_steal)
    LinearLayout mSteal;
    @InjectView(R.id.my_kefu)
    LinearLayout mKefu;
    @InjectView(R.id.my_trancfer)
    LinearLayout mTancfer;
//    @InjectView(R.id.histogramchart)
//    Histogram histogramChart;
    @InjectView(R.id.spreadBarChart1)
    BarChart mBarChart1;
    private BarCharts mBarCharts;
    private ActionSheetDialog dialog;

    int[] ydata = new int[7];
    private  List<WeekToCount> data;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frgt_my, null);
        ButterKnife.inject(this,view);
        initDatas();
        initData();
     //   initView();



        return view;
    }

//   private void initView() {
//        update();
//        final String[] xdata = new String[]{"周一","周二","周三","周四","周五","周六","周日"};
//        //final Histogram histogramChart = (Histogram) view.findViewById(R.id.histogramchart);
//        final HistogramData histogramData = HistogramData.builder()
//                .setXdata(xdata)
//                .setYdata(ydata)
//                .setYpCount(7)
//                .setAnimType(Anim.ANIM_NONE)
//                .build();
//        histogramChart.setChartData(histogramData);
//
//    }

    private void update(){
        for (int i=0;i<7;i++){
            ydata[i] = (new Random().nextInt(100));
        }
    }


    private void initData() {
        RequestParams params = new RequestParams.Builder()
                .build();

        //开始请求发送数据到服务器服务器
        RequestQueue requestQueue = Volley.newRequestQueue(this.getActivity());
        String beanJson= SPUtil.getInstance().getData("beanJson");
        String url = Urls.getWeek()+"?beanJson="+beanJson.replace(" ","%20");//回去api路径

        RequestUtil.requestData(requestQueue,
                IConstants.LOGIN,
                url,
                RM.GET,
                RequestBean.class,
                params,
                this);
        //设置图标
        setIcon();
    }

    private void setIcon() {
       // Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.mv2);
        //mIcon.setImageBitmap(RoundBitmap.toRoundBitmap(bitmap));
        mIcon.setImageResource(R.drawable.mv2);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
   // R.id.my_sales_service, R.id.my_collect, R.id.my_feedback,
    @OnClick({R.id.my_icon,R.id.bind_car, R.id.my_message,  R.id.my_setting, R.id.my_about,R.id.my_steal,R.id.my_trancfer,R.id.my_kefu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bind_car:
                //绑定设备
                startToAty(BindCarAty.class);
               // startToAty(Testnew.class);
                break;
            case R.id.my_message:
                startToAty(DispathActivity.class);
                //startToAty();
                break;
           /* case R.id.my_sales_service:
                startToAty(AftersalesActivity.class);
                break;
            case R.id.my_collect:
                startToAty(MycollctActivity.class);
                break;*/
            case R.id.my_kefu:{
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("确认要拨打客服电话:18378307553？");
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+"18378307553"));
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
                break;
            }
            case R.id.my_steal:{
                startToAty(StealActivity.class);
                break;
            }
            case R.id.my_trancfer:{
                startToAty(TrancferActivty.class);
                break;
            }
//            case R.id.my_feedback:
//                startToAty(FedbackActivity.class);
//                break;
            case R.id.my_setting:
                startToAty(SettingActivity.class);
                break;
            case R.id.my_about:
                startToAty(AboutActivity.class);
                break;
           /* case R.id.my_icon:
                //上传图标
                dialog = new ActionSheetDialog(MyFrgt.this.getActivity());
                dialog.builder()
                        .setTitle("请选择头像")
                        .setCancelable(false)
                        .setCanceledOnTouchOutside(false)
                        .addSheetItem("相册", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {
                                        Intent intent = new Intent(Intent.ACTION_PICK, null);
                                        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                                        startActivityForResult(intent, 1);
                                    }
                                })
                        .addSheetItem("拍照", ActionSheetDialog.SheetItemColor.Blue,
                                new ActionSheetDialog.OnSheetItemClickListener() {
                                    @Override
                                    public void onClick(int which) {

                                    }
                                }).show();
                break;*/
        }
    }

    private void startToAty(Class claz) {
        Intent intent = new Intent(this.getActivity(), claz);
        startActivity(intent);
    }
    private void initDatas() {
        mBarCharts = new BarCharts();


        XAxis xAxis = mBarChart1.getXAxis();

        YAxis yAxis = mBarChart1.getAxisLeft();
        YAxis yAxis2 = mBarChart1.getAxisRight();
        yAxis.setEnabled(false);
        yAxis2.setEnabled(false);
        yAxis.setDrawGridLines(false);
        xAxis.setEnabled(true);
        xAxis.setTextColor(Color.parseColor("#ffcc0000"));
//        xAxis.setLabelsToSkip(0);
//        xAxis.setSpaceBetweenLabels(0);
        //   yAxis.setLabelCount(20,false);
       mBarChart1.setVisibleXRangeMinimum(1f);
       // mBarChart1.setDrawValueAboveBar(false);
        //  xAxis.

        //      mBarCharts.showBarChart(mBarChart1, getBarData(20, 199), true);
    }
    public BarData getBarData(int count, List<WeekToCount> data) {

        ArrayList<String> xValues = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            xValues.add(data.get(i).getWeekName());
        }

        ArrayList<BarEntry> yValues = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            // float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
            double  value= data.get(i).getCountLicheng();
           // int value=i;
            //int value=i;
            yValues.add(new BarEntry((float) value, i));
        }
        // y轴的数据集合
        BarDataSet barDataSet = new BarDataSet(yValues, "测试图");
       // barDataSet.setBarSpacePercent(10f);

        ArrayList<Integer> colors = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            colors.add(Color.parseColor("#ffcc0000"));
        }
        barDataSet.setColors(colors);
        // 设置栏阴影颜色
        barDataSet.setBarShadowColor(Color.parseColor("#01000000"));
        ArrayList<BarDataSet> barDataSets = new ArrayList<>();
        barDataSets.add(barDataSet);
        barDataSet.setValueTextColor(Color.parseColor("#FF8F9D"));
        // 绘制值
        barDataSet.setDrawValues(true);
       // barDataSet.setLabel("111");
        BarData barData = new BarData(xValues, barDataSets);
        return barData;
    }
    @Override
    public void requestSuccess(RequestBean bean, Config config) {
        LogUtil.e("requestSuccess数据="+bean.beanJson);
        data = new Gson().fromJson(bean.beanJson,new TypeToken<List<WeekToCount>>(){}.getType());
        Iterator it = data.iterator();
        while(it.hasNext()) {
            LogUtil.e(it.next().toString());
        }
        if(mBarChart1!=null){
        mBarCharts.showBarChart(mBarChart1, getBarData(7,data), true);
        }
//       for (int i =0 ;i<7;i++){
//           ydata[i]= data.get(i).getCountLicheng();
//           LogUtil.e(String.valueOf(ydata[i]));
//       }
//       initView();

    }

    @Override
    public void requestFail(RequestBean bean, Config config) {

    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {

    }
}
